<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/5/14
 * Time: 10:52
 */
namespace Game\Models\Common;
use Game\Lib\Game;
use Game\Module\Mysql;
use Game\Lib\GlobalConst;
trait MysqlData{
    /**
     * 实例化mmsql
     * @return \Workerman\MySQL\Connection
     */
    public function initMysql(){
        return Mysql::instance("DB");
    }

    /**
     * close mmsql
     */
    public function closeMysql(){
        Mysql::close("DB");
    }

    /**
     * 扣除房卡或退还房卡
     * @param $room_id
     * @param $account_id
     * @param $spend_ticket_count
     * @return bool
     */
    protected function balanceTicket($room_id, $account_id, $spend_ticket_count){
        $timestamp = time();
        $mMysql = $this->initMysql();

        $ticket_count = $mMysql->from(Wechat_Account)->select("ticket_count")->where("id=".$account_id)->single();
        $balance = $ticket_count - $spend_ticket_count;
        if($balance < 0) {
//            $this->writeLog("balance negative balance: ".$balance." account_id: ".$account_id." room_id: ".$room_id);
//            $journal_array['balance'] = 0;
            return false;
        }
        $mMysql->update(Wechat_Account)->set("ticket_count", $balance)->where("id=".$account_id)->query();

        //添加到流水账
        $journal_array['create_time'] = $timestamp;
        $journal_array['create_id'] = $account_id;
        $journal_array['update_time'] = $timestamp;
        $journal_array['update_id'] = $account_id;
        $journal_array['is_delete'] = GlobalConst::IS_FALSE;
        $journal_array['account_id'] = $account_id;
        $journal_array['object_id'] = $room_id;
        $journal_array['object_type'] = 3;  //游戏
        $journal_array['balance'] = $balance;
        $journal_array['extra'] = "";

        if($spend_ticket_count > 0){
            $journal_array['disburse'] = $spend_ticket_count;
            $journal_array['abstract'] = Game::Game_Name;		//摘要
        } else {
            $journal_array['income'] = -$spend_ticket_count;
            $journal_array['abstract'] = Game::Game_Name."房卡退还";		//摘要
        }
        $mMysql->insert(Account_Ticket_Journal)->cols($journal_array)->query();
        return true;
    }

    protected function getGameAnnouncement($account_id){
        $timestamp = time();
        $MMYSQL = $this->initMysql();
        $announcement_where = 'game_type='.Game::Game_Type.' and announce_time<='.$timestamp.' and end_time>'.$timestamp.' and is_delete=0';
        $announcement_query = $MMYSQL->select("announce_time,service_time,end_time,announce_text,service_text")->from(Game_Announcement)->where($announcement_where)->row();
        if(is_array($announcement_query) && count($announcement_query) > 0) {
            $service_time = $announcement_query['service_time'];
            $announce_text = $announcement_query['announce_text'];
            $service_text = $announcement_query['service_text'];

            if($timestamp >= $service_time) {
                $array['alert_type'] = 4;
                $array['alert_text'] = $service_text;
            } else {
                $array['alert_type'] = 4;
                $array['alert_text'] = $announce_text;
            }
            //获取白名单用户
            $whilelist_query = $MMYSQL->select("data_id")->from(Game_Whilelist)->where('account_id='.$account_id.' and is_delete=0')->single();
            if($whilelist_query) {
                $this->logMessage('error', "function(getGameAnnouncement):白名单用户:".$account_id." in file".__FILE__." on Line ".__LINE__);
                return true;
            } else {
                return $array;
            }
        }
        return true;
    }



    /**
     * 获取房卡余额
     * @param $account_id
     * @return int|string
     */
    protected function getTicketCount($account_id){
        $mMysql = $this->initMysql();
        return $mMysql->select("ticket_count")->from(Wechat_Account)->where("id=" . $account_id)->single();
    }

    /**
     * 判断房间申请记录是否存在,防止重复提交
     * @param $data_key
     * @return array|bool
     */
    protected function getRoomByDataKey($data_key){
        return false;
        $mMysql = $this->initMysql();
        return $mMysql->from(Room_Game)->select("*")->where('data_key="' . $data_key . '"')->row();
    }

    protected function getRoomByRoomNumber($room_number, $is_close = null){
        $mMysql = $this->initMysql();
        $where = 'room_number="' . $room_number . '"';
        if ($is_close !== null){
            $where .= " and is_close =".$is_close;
        }
        return $mMysql->from(Room_Game)->select("*")->where($where)->row();
    }
    protected function getRoomByRoomID($id){
        $mMysql = $this->initMysql();
        return $mMysql->from(Room_Game)->select("*")->where('id="' . $id . '"')->row();
    }

    protected function insertRoomGameResult($array){
        $mMysql = $this->initMysql();
        return $mMysql->insert(Room_GameResult)->cols($array)->query();
    }
    protected function insertRoomGameScore($array){
        $mMysql = $this->initMysql();
        return $mMysql->insert(Room_GameScore)->cols($array)->query();
    }
    protected function insertRoomScoreboard($array){
        $mMysql = $this->initMysql();
        return $mMysql->insert(Room_Scoreboard)->cols($array)->query();
    }
    /**
     * 插入房间表
     * @param $room_array
     * @return mixed
     */
    protected function insertRoom($room_array){
        $mMysql = $this->initMysql();
        return $mMysql->insert(Room_Game)->cols($room_array)->query();
    }

    protected function updateRoom($room_id,$room_array){
        $mMysql = $this->initMysql();
        return $mMysql->update(Room_Game)->cols($room_array)->where("id = ".$room_id)->query();
    }

    protected function getAccountByID($account_id){
        $mMysql = $this->initMysql();
        return $mMysql->from(Wechat_Account)->select("*")->where("account_id=".$account_id)->row();
    }

    protected function getAccountNickName($account_id){
        $mMysql = $this->initMysql();
        return $mMysql->from(Wechat_Account)->select("nickname")->where('account_id ='.$account_id)->single();
    }

    protected function getAccountListByIDs($account_ids){
        $mMysql = $this->initMysql();
        return $mMysql->from(Wechat_Account)->select("*")->where("account_id in(".implode(",", $account_ids).")")->query();
    }

    protected function getRoomScoreboardByRoomID($room_id){
        $mMysql = $this->initMysql();
        return $mMysql->from(Room_Scoreboard)->select("*")->where('room_id='.$room_id.' and game_type='.Game::Game_Type)->orderByDESC(['create_time'])->query();
    }

    protected function getGameTypeInfo(){
        $mMysql = $this->initMysql();
        $game_type = $mMysql->from(Game_Type)->select("*")->where('id='.Game::Game_Type)->orderByDESC(['create_time'])->single();
        if ($game_type){
            $game_type["group"] = $mMysql->from(Game_Type_Group)->select("*")->where("game_type =" .Game::Game_Type)->query();
            foreach ($game_type["group"] as &$item) {
                $item['groupitem'] = $mMysql->from(Game_Type_Groupitem)->select("*")->where("group_id =" .$item['id'])->query();
            }
        }
    }

    protected function checkTicketUse($data){
        $mMysql = $this->initMysql();
        $ticket_use = $mMysql->from(Game_Type_Tickuse)->select("*")->where("game_type =" .Game::Game_Type)->query();
        
    }

}